#
# This is a PERL source file of validation routines and help texts for
# the Friends databas of the Linux Counter.
#

package Validate::Friends;

use Validate;
use Validate::Persons;

@ISA = qw(Validate);

%help = (
);

sub help {
   my ($self, $field) = @_;

   return $help{$field};
}

sub check {
   my ($self, $field, $value) = @_;

   if ($field eq "name") {
       if ($value eq "") {
	   return "You cannot leave the name field blank!"
       }
   }
   ""; # If we get here - no error
}

sub cleanfield {
    my($self, $field, $value) = @_;
    my($lower, $origval);

    $origval = $value;
    # Generic fieldstripping: Strip quotes and trailing punctuation
    if ($value =~ /^\s*"(.*)"\s*$/) {
	$value = $1;
    }
    $value =~ s/[ \.?]+$//;

    # NOTE: The synonyms book should go here
    # Tag specific stripping
    if ($field eq "country") {
	# Max length 2 uppercase chars
	$value =~ tr/a-z/A-Z/;

	$value = substr($value, 0, 2); # Bad....
    } elsif ($field eq "started") {
	$value = cleanmonthyear($value);
    }
    if ($value eq "" && $origval ne "") {
	$self->notestrange($field, $origval);
    }
    $value;
}

sub clean {
    my ($self, $rec) = @_;
    my $value = {};
    for $field (keys(%$rec)) {
	# Skip reserved fields
	if ($field !~ /^\*/) {
	    $$value{$field} = $self->cleanfield($field, $$rec{$field});
	}
    }
    # Inter-field patching
    if (!$$value{country} && $$value{name}) {
	# Inter-field patching
	$$value{country} = Validate::Persons::email2country($$value{name});
    }
    return $value;
}

sub notestrange {
    my($self, $field, $value) = @_;
    warn "Strange value: $field $value\n";
}


1;
